package com.atguigu.eduservice.controller;


import cn.hutool.core.util.StrUtil;
import com.atguigu.commonutils.JwtUtil;
import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.service.EduCommentService;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author test.java
 * @since 2023-03-23
 */
@Api(value="用户评论")
@RestController
@RequestMapping("/eduservice/edu-comment")
@CrossOrigin
public class EduCommentController {
	@Resource
	private EduCommentService eduCommentService;

	//实现添加评论的功能
	@GetMapping("/addComments")
	public Result addComments(@RequestParam("comment") String comment,
							  @RequestParam("") String courseId,
							  HttpServletRequest request){

		Result result=eduCommentService.saveEduComment(comment,courseId,request);

		return result;
	}

	@PostMapping("/page/selectComments")
	public Result getComments(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
							  @ApiParam(name = "limit", value = "每页记录数", required = true)
							   @PathVariable Long limit){

		Page<EduComment> eduCommentPage=new Page<>(page,limit);
		LambdaQueryWrapper<EduComment> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		//根据评论的创建时间排序
		lambdaQueryWrapper.orderByDesc(EduComment::getGmtCreate);
		eduCommentService.page(eduCommentPage, lambdaQueryWrapper);
		List<EduComment> records = eduCommentPage.getRecords();
		long total = eduCommentPage.getTotal();
		return Result.ok().data("records",records).data("total",total);

	}

}

